package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberBusinessBinding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.ui.activity.MemberBusinessActivity;
import com.sgb.member.view_model.MemberBusinessFragModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 会员经营自页面样式
 * {"经营商品", "招采经营", "集采经营", "租赁经营", "劳务工队", "物流货运", "企业服务"}
 */
public class MemberBusinessFragment extends AppLazyFragment {

    @Inject
    public MemberBusinessFragModel sModel;
    private FragmentMemberBusinessBinding mBinding;
    private List<CommonPopEntity> mTabData;
    private int mCurPos;
    private List<Fragment> mFragments;
    int selectedTabPosition;

    public static MemberBusinessFragment newInstance(int position) {
        MemberBusinessFragment fragment = new MemberBusinessFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_business, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
    }

    @Override
    protected void onLazyLoad() {
        loadTabData();
    }

    public void loadData() {
        boolean hasTab = loadTabData();
        if (!hasTab) {
            return;
        }
        if (mBinding.tabLayout.getVisibility() == View.VISIBLE) {
            selectedTabPosition = mBinding.tabLayout.getSelectedTabPosition();
        } else {
            selectedTabPosition = 0;
        }
        Fragment fragment = mFragments.get(selectedTabPosition);
        switch (mCurPos) {
            case 0:
                // 会员商品
                if (fragment instanceof BusinessGoodsFragment) {
                    ((BusinessGoodsFragment) fragment).loadData(false);
                } else if (fragment instanceof BusinessGoodsFragment2) {
                    ((BusinessGoodsFragment2) fragment).loadData(false);
                } else if (fragment instanceof BusinessGoodsFragment3) {
                    ((BusinessGoodsFragment3) fragment).loadData(false);
                }
                break;
            case 1:
                // 会员招采
                if (fragment instanceof BusinessTenderFragment) {
                    ((BusinessTenderFragment) fragment).loadData(false);
                }
                break;
            case 2:
                // 会员集采
                if (fragment instanceof BusinessCollectingFragment) {
                    ((BusinessCollectingFragment) fragment).loadData(false);
                }
                break;
//            case 3:
//                // 会员租赁
//                if (fragment instanceof BusinessLeaseFragment) {
//                    ((BusinessLeaseFragment) fragment).loadData(false);
//                }
//                break;
            case 4:
                // 劳务工队
                if (fragment instanceof BusinessTeamFragment) {
                    ((BusinessTeamFragment) fragment).loadData(false);
                } else if (fragment instanceof MemberTeamPersonFragment) {
                    ((MemberTeamPersonFragment) fragment).loadData(false);
                }
                if (fragment instanceof BusinessTeamFragment2) {
                    ((BusinessTeamFragment2) fragment).loadData(false);
                }
                break;
            case 5:
                // 物流货物
                if (fragment instanceof BusinessLogisticsFragment) {
                    ((BusinessLogisticsFragment) fragment).loadData(false);
                }
                break;
            case 6:
                // 企业服务
                if (fragment instanceof BusinessEnterpriseFragment) {
                    ((BusinessEnterpriseFragment) fragment).loadData(false);
                }
                break;
            default:
                break;
        }
    }

    public boolean loadTabData() {
        if (mTabData == null || mTabData.size() == 0) {
            switch (mCurPos) {
                case 0:
                    // 会员商品
                    if (MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_COMPANY) {
                        sModel.getGoodsType();
                    } else {
                        mBinding.tabLayout.setVisibility(View.GONE);
                        List<CommonPopEntity> data = new ArrayList<>();
                        data.add(new CommonPopEntity());
                        setTabLayoutData(data);
                    }
                    break;
                case 5:
                    // 5 物流货物
//                    sModel.getLogisticsType();
                    // 0 会员商品 v1.1.1取消行业分类筛选
                    /*sModel.getGoodsCategory(PickCateType.TYPE_0);*/
//                    mBinding.tabLayout.setVisibility(View.GONE);
//                    List<CommonPopEntity> data = new ArrayList<>();
//                    data.add(new CommonPopEntity());
//                    setTabLayoutData(data);
                    sModel.getWuliuType();
                    break;
                case 1:
                    // 会员招采
                    sModel.getPickType();
                    break;
                case 2:
                    // 会员集采
                    sModel.getCollectingType();
                    break;
//                case 3:
//                    // 会员租赁
//                    sModel.getLeaseType();
//                    break;
                case 4:
                    // 会员工队
                    sModel.getPackerType();
                    break;
                case 6:
                    // 企业服务
                    sModel.getEnterpriseServicesType();
                    break;
                default:
                    break;
            }
            return false;
        }
        return true;
    }

    // 更新顶部Tab数据
    public void setTabLayoutData(List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) {
            mBinding.emptyView.setVisibility(View.VISIBLE);
            sModel.showOrHideDialog(false);
            return;
        }
        mBinding.emptyView.setVisibility(View.GONE);
        mTabData = new ArrayList<>();
        mTabData.addAll(data);
        refreshTabs();
    }

    private void refreshTabs() {
        mBinding.viewPager.removeAllViews();
        mBinding.viewPager.clearOnPageChangeListeners();
        mBinding.tabLayout.removeAllTabs();
        int size = mTabData.size();
        mBinding.tabLayout.setVisibility(View.VISIBLE);
        mFragments = new ArrayList<>();
        switch (mCurPos) {
            case 0:
                // 会员商品
                if (MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_PERSONAL) {
                    mBinding.tabLayout.setVisibility(View.GONE);
                    mFragments.add(BusinessGoodsFragment.newInstance(0, mTabData.get(0)));
                } else {
                    for (int i = 0; i < size; i++) {
                        if (i == 0) {//供方会员报价
                            mFragments.add(BusinessGoodsFragment3.newInstance(i, mTabData.get(i)));
                        } else {//公司经营
                            mFragments.add(BusinessGoodsFragment2.newInstance(i, mTabData.get(i)));
                        }
                    }
                }
                break;
            case 1:
                // 会员招采
                for (int i = 0; i < size; i++) {
                    mFragments.add(BusinessTenderFragment.newInstance(i, mTabData.get(i)));
                }
                break;
            case 2:
                // 会员集采
                for (int i = 0; i < size; i++) {
                    mFragments.add(BusinessCollectingFragment.newInstance(i, mTabData.get(i)));
                }
                break;
            case 3:
//                // 会员租赁
//                mBinding.tabLayout.setVisibility(View.GONE);
//                mFragments.add(BusinessLeaseFragment.newInstance());
                break;
            case 4:
                // 会员工队
                for (int i = 0; i < size; i++) {
                    switch (mTabData.get(i).getNo()) {
                        case "0":
                        case "1":
                            mFragments.add(BusinessTeamFragment.newInstance(i, mTabData.get(i)));
                            break;
                        case "2":
                            mFragments.add(MemberTeamPersonFragment.newInstance(i, mTabData.get(i)));
                            break;
                        case "3"://工种报价
                            mFragments.add(BusinessTeamFragment2.newInstance(i, mTabData.get(i)));
                            break;
                    }
                }
                break;
            case 5:
                // 物流货物
                mFragments.add(BusinessLogisticsFragment.newInstance());//公司货运/个人货运
                mFragments.add(BusinessLogisticsFragment2.newInstance());//会员货运
                mFragments.add(BusinessLogisticsFragment3.newInstance());//车辆管理
                mFragments.add(BusinessLogisticsFragment4.newInstance());//公司长期货运/个人长期货运
                break;
            case 6:
                // 企业服务
                for (int i = 0; i < size; i++) {
                    mFragments.add(BusinessEnterpriseFragment.newInstance(i, mTabData.get(i)));
                }
                break;
            default:
                break;
        }
        mBinding.viewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mTabData.get(position).getPickerViewText();
            }
        });
//        mBinding.viewPager.setOffscreenPageLimit(mFragments.size());
        if (mBinding.tabLayout.getVisibility() == View.VISIBLE) {
            mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
            if (mCurPos == 1 || mCurPos == 2 || mCurPos == 4) {
                mBinding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
            } else {
                mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            }
            mBinding.tabLayout.setNormalColor(R.color.color_333333);
            mBinding.tabLayout.addOnTabSelectedListener(mTabData, new XCustomTabLayout.SimpleSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mBinding.viewPager.setCurrentItem(tab.getPosition());
                    setHintText(tab.getPosition());
                }
            });
        }
    }

    /**
     * 修改父Activity中搜索的默认显示
     *
     * @param Pos
     */
    public void setHintText(int Pos) {
        String EditTextHint = "";
        if (mFragments.get(Pos) != null) {
            Fragment fragment = mFragments.get(Pos);
            //公司经营页面
            if (fragment != null && fragment instanceof BusinessGoodsFragment2) {
                EditTextHint = "请输入商品名称";
            }
            //供方会员报价页面
            if (fragment != null && fragment instanceof BusinessGoodsFragment3) {
                EditTextHint = "请输入企业名称";
            }
            if (getActivity() instanceof MemberBusinessActivity) {
                MemberBusinessActivity memberbusinessactivity = (MemberBusinessActivity) getActivity();
                memberbusinessactivity.setHintText(EditTextHint);
            }
        }
    }
}
